package com.zimin.mapper.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.zimin.common.utils.ServletUtil;
import com.zimin.mapper.constant.MybatisPlusConstant;
import org.apache.ibatis.reflection.MetaObject;

import java.time.LocalDateTime;

/**
 * @author zimin
 * @since 2021/10/14 2:23 下午
 */
public class MybatisPlusMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, MybatisPlusConstant.CREATE_TIME, LocalDateTime::now, LocalDateTime.class);
        this.strictUpdateFill(metaObject, MybatisPlusConstant.CREATE_BY, String.class, ServletUtil.getUserName());
        this.strictUpdateFill(metaObject, MybatisPlusConstant.UPDATE_TIME, LocalDateTime::now, LocalDateTime.class);
        this.strictUpdateFill(metaObject, MybatisPlusConstant.UPDATE_BY, String.class, ServletUtil.getUserName());

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, MybatisPlusConstant.UPDATE_TIME, LocalDateTime::now, LocalDateTime.class);
        this.strictUpdateFill(metaObject, MybatisPlusConstant.UPDATE_BY, String.class, ServletUtil.getUserName());
    }
}
